package org.wu.spring.db.integration.compare.application.impl;

import jakarta.annotation.Resource;
import org.wu.framework.database.lazy.web.plus.stereotype.LazyApplication;
import org.wu.framework.lazy.orm.database.lambda.domain.LazyPage;
import org.wu.framework.web.response.Result;
import org.wu.framework.web.response.ResultFactory;
import org.wu.spring.db.integration.compare.application.DbCompareRecordsApplication;
import org.wu.spring.db.integration.compare.application.assembler.OrmCompareRecordsDTOAssembler;
import org.wu.spring.db.integration.compare.application.command.orm.compare.records.*;
import org.wu.spring.db.integration.compare.application.dto.DbCompareRecordsDTO;
import org.wu.spring.db.integration.compare.domain.model.orm.compare.records.DbCompareRecords;
import org.wu.spring.db.integration.compare.domain.model.orm.compare.records.DbCompareRecordsEcharts;
import org.wu.spring.db.integration.compare.domain.model.orm.compare.records.DbCompareRecordsRepository;
import org.wu.spring.db.integration.compare.enums.DatabaseType;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;


/**
 * describe Orm 操作记录
 *
 * @author Jia wei Wu
 * @date 2024/02/28 01:02 下午
 **/
@LazyApplication
public class DbCompareRecordsApplicationImpl implements DbCompareRecordsApplication {

    @Resource
    DbCompareRecordsRepository dbCompareRecordsRepository;

    /**
     * describe 新增Orm 操作记录
     *
     * @param dbCompareRecordsStoryCommand 新增Orm 操作记录
     * @return {@link Result< DbCompareRecords >} Orm 操作记录新增后领域对象
     * @author Jia wei Wu
     * @date 2024/02/28 01:02 下午
     **/

    @Override
    public Result<DbCompareRecords> story(DbCompareRecordsStoryCommand dbCompareRecordsStoryCommand) {
        DbCompareRecords dbCompareRecords = OrmCompareRecordsDTOAssembler.INSTANCE.toOrmCompareRecords(dbCompareRecordsStoryCommand);
        return dbCompareRecordsRepository.story(dbCompareRecords);
    }

    /**
     * describe 批量新增Orm 操作记录
     *
     * @param dbCompareRecordsStoryCommandList 批量新增Orm 操作记录
     * @return {@link Result<List< DbCompareRecords >>} Orm 操作记录新增后领域对象集合
     * @author Jia wei Wu
     * @date 2024/02/28 01:02 下午
     **/

    @Override
    public Result<List<DbCompareRecords>> batchStory(List<DbCompareRecordsStoryCommand> dbCompareRecordsStoryCommandList) {
        List<DbCompareRecords> dbCompareRecordsList = dbCompareRecordsStoryCommandList.stream().map(OrmCompareRecordsDTOAssembler.INSTANCE::toOrmCompareRecords).collect(Collectors.toList());
        return dbCompareRecordsRepository.batchStory(dbCompareRecordsList);
    }

    /**
     * describe 更新Orm 操作记录
     *
     * @param ormCompareRecordsUpdateCommand 更新Orm 操作记录
     * @return {@link Result< DbCompareRecords >} Orm 操作记录领域对象
     * @author Jia wei Wu
     * @date 2024/02/28 01:02 下午
     **/

    @Override
    public Result<DbCompareRecords> updateOne(OrmCompareRecordsUpdateCommand ormCompareRecordsUpdateCommand) {
        DbCompareRecords dbCompareRecords = OrmCompareRecordsDTOAssembler.INSTANCE.toOrmCompareRecords(ormCompareRecordsUpdateCommand);
        return dbCompareRecordsRepository.story(dbCompareRecords);
    }

    /**
     * describe 查询单个Orm 操作记录
     *
     * @param ormCompareRecordsQueryOneCommand 查询单个Orm 操作记录
     * @return {@link Result< DbCompareRecordsDTO >} Orm 操作记录DTO对象
     * @author Jia wei Wu
     * @date 2024/02/28 01:02 下午
     **/

    @Override
    public Result<DbCompareRecordsDTO> findOne(OrmCompareRecordsQueryOneCommand ormCompareRecordsQueryOneCommand) {
        DbCompareRecords dbCompareRecords = OrmCompareRecordsDTOAssembler.INSTANCE.toOrmCompareRecords(ormCompareRecordsQueryOneCommand);
        return dbCompareRecordsRepository.findOne(dbCompareRecords).convert(OrmCompareRecordsDTOAssembler.INSTANCE::fromOrmCompareRecords);
    }

    /**
     * describe 查询多个Orm 操作记录
     *
     * @param ddCompareRecordsQueryListCommand 查询多个Orm 操作记录
     * @return {@link Result<List< DbCompareRecordsDTO >>} Orm 操作记录DTO对象
     * @author Jia wei Wu
     * @date 2024/02/28 01:02 下午
     **/

    @Override
    public Result<List<DbCompareRecordsDTO>> findList(DdCompareRecordsQueryListCommand ddCompareRecordsQueryListCommand) {
        DbCompareRecords dbCompareRecords = OrmCompareRecordsDTOAssembler.INSTANCE.toOrmCompareRecords(ddCompareRecordsQueryListCommand);
        return dbCompareRecordsRepository.findList(dbCompareRecords).convert(ormCompareRecordss -> ormCompareRecordss.stream().map(OrmCompareRecordsDTOAssembler.INSTANCE::fromOrmCompareRecords).collect(Collectors.toList()));
    }

    /**
     * describe 分页查询多个Orm 操作记录
     *
     * @param ddCompareRecordsQueryListCommand 分页查询多个Orm 操作记录
     * @return {@link Result<LazyPage< DbCompareRecordsDTO >>} 分页Orm 操作记录DTO对象
     * @author Jia wei Wu
     * @date 2024/02/28 01:02 下午
     **/

    @Override
    public Result<LazyPage<DbCompareRecordsDTO>> findPage(int size, int current, DdCompareRecordsQueryListCommand ddCompareRecordsQueryListCommand) {
        DbCompareRecords dbCompareRecords = OrmCompareRecordsDTOAssembler.INSTANCE.toOrmCompareRecords(ddCompareRecordsQueryListCommand);
        return dbCompareRecordsRepository.findPage(size, current, dbCompareRecords).convert(page -> page.convert(OrmCompareRecordsDTOAssembler.INSTANCE::fromOrmCompareRecords));
    }

    /**
     * describe 删除Orm 操作记录
     *
     * @param dbCompareRecordsRemoveCommand 删除Orm 操作记录
     * @return {@link Result< DbCompareRecords >} Orm 操作记录
     * @author Jia wei Wu
     * @date 2024/02/28 01:02 下午
     **/

    @Override
    public Result<DbCompareRecords> remove(DbCompareRecordsRemoveCommand dbCompareRecordsRemoveCommand) {
        DbCompareRecords dbCompareRecords = OrmCompareRecordsDTOAssembler.INSTANCE.toOrmCompareRecords(dbCompareRecordsRemoveCommand);
        return dbCompareRecordsRepository.remove(dbCompareRecords);
    }

    /**
     * describe 获取echarts数据
     */
    @Override
    public Result<DbCompareRecordsEcharts> findEchartsData(String type) {
        DbCompareRecordsEcharts dbCompareRecordsEcharts = new DbCompareRecordsEcharts();
        dbCompareRecordsEcharts.setDatabaseTypeList(Arrays.stream(DatabaseType.values()).map(Enum::name).toList());
        DbCompareRecords queryUrmCompareRecordsCondition = new DbCompareRecords().setType(type);
        //
        List<List<Object>> dataList = dbCompareRecordsRepository.findList(queryUrmCompareRecordsCondition)
                .applyOther(dbCompareRecordsList -> dbCompareRecordsList
                        .stream()
                        .sorted(Comparator.comparing(DbCompareRecords::getAffectsRows))
                        .map(dbCompareRecords -> {
                            String databaseType = dbCompareRecords.getDatabaseType();
                            Integer affectsRows = dbCompareRecords.getAffectsRows();
                            Integer affectsColumn = dbCompareRecords.getAffectsColumn();
                            Long operationTime = dbCompareRecords.getOperationTime();
                            List<Object> data = new ArrayList<>();
                            //
                            data.add(operationTime);
                            data.add(operationTime);
                            data.add(operationTime);
                            data.add(databaseType);
                            data.add(affectsColumn);
                            return data;
                        }).toList());
        dbCompareRecordsEcharts.addDataList(dataList);

        return ResultFactory.successOf(dbCompareRecordsEcharts);
    }
}